home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Miscellaneous
/
Headlines Code
/
Jamie’s PD Code ƒ
/
JRandom.c
< prev
next >
Wrap
Text File
|
1992-06-20
|
2KB
|
80 lines
/*
* JRandom.c
*
* "JamieRandom": A hacked-up non-OOP version of version 1.1.1 of CRandom.
* CRandom is available by ftp from ftp.brown.edu in /pub/tcl/classes.
*
* JRandom.c and JRandom.h are in the public domain.
*
*/
/******************************/
#include "JRandom.h"
/******************************/
#include <Global.h>
/******************************/
#define MULTIPLICAND (4096)
#define MULTIPLY(x) ((x) << 12)
#define ADDEND (150889)
#define ADD(x) ((x) + ADDEND)
/* this probably isn't really a word, but what the hell */
#define MODULEND (NDISTINCTRANDOMVALUES)
#define MODULO(x) ((x) % MODULEND)
#define NEXTVAL(x) (MODULO(ADD(MULTIPLY(x))))
/******************************/
void IJRandom(jrPtr theJRPtr)
{
unsigned long theDateTime;
GetDateTime(&theDateTime);
jrSeed(theJRPtr, theDateTime ^ TickCount());
}
void jrSeed(register jrPtr theJRPtr,
unsigned long seedVal)
{
register short j;
theJRPtr->dummy = MODULO(Abs(ADDEND - seedVal));
for (j = NRANDOMSLOTS-1; j >= 0; j--) {
theJRPtr->seeds[j] = theJRPtr->dummy = NEXTVAL(theJRPtr->dummy);
}
theJRPtr->value = theJRPtr->dummy = NEXTVAL(theJRPtr->dummy);
}
short jrLinearShort(register jrPtr theJRPtr,
register short lowest, short highest)
{
register long interval = highest - lowest + 1;
register long partitionSize;
register short j;
if (interval <= 0) return lowest;
partitionSize = (NDISTINCTRANDOMVALUES-1)/interval;
while (TRUE) {
j = (NRANDOMSLOTS*theJRPtr->value) / NDISTINCTRANDOMVALUES;
theJRPtr->value = theJRPtr->seeds[j];
theJRPtr->seeds[j] = theJRPtr->dummy = NEXTVAL(theJRPtr->dummy);
if (theJRPtr->value < partitionSize * interval) {
return lowest + theJRPtr->value/partitionSize;
}
}
}